#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cstdlib>
#include "sdlglutils.h"
#include "trackballcamera.h"

void DrawCube();
void DrawRocket();

double angleX=0.0;
double angleZ=0.0;

GLuint texture1, texture2,textureTop,textureMiddle,textureBottom,textureMotor;
TrackBallCamera * camera;

void stop()
{
    delete camera;
    SDL_Quit();
}

int main(int argc, char *argv[])
{
    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);
    atexit(stop);
    SDL_WM_SetCaption("SDL GL Application", NULL);
    SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    gluPerspective(70,(double)640/480,1,1000);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);
    textureTop = loadTexture("textures/rocket_top.jpg");
    textureMiddle = loadTexture("textures/rocket_middle.jpg");
    textureBottom = loadTexture("textures/rocket_bottom.jpg");
    textureMotor = loadTexture("textures/rocket_motor.jpg");

    texture1 = loadTexture("textures/stainedglass05.jpg");
    texture2 = loadTexture("textures/checker02.jpg");
    Uint32 last_time = SDL_GetTicks();
    Uint32 current_time,ellapsed_time;

    camera = new TrackBallCamera();
    camera->setScrollSensivity(0.1);

    for(;;)
    {
        while (SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                exit(0);
                break;
            case SDL_KEYDOWN:
                switch (event.key.keysym.sym)
                {
                case SDLK_p:
                    takeScreenshot("test.bmp");
                    break;
                case SDLK_ESCAPE:
                    exit(0);
                    break;
                default :
                    camera->OnKeyboard(event.key);
                }
                break;
            case SDL_MOUSEMOTION:
                camera->OnMouseMotion(event.motion);
                break;
            case SDL_MOUSEBUTTONUP:
            case SDL_MOUSEBUTTONDOWN:
                camera->OnMouseButton(event.button);
                break;
            }
        }

        current_time=SDL_GetTicks();
        ellapsed_time=current_time-last_time;
        last_time=current_time;

        angleX+=0.05*ellapsed_time;
        angleZ+=0.05*ellapsed_time;

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity( );
  camera->look();
     //   gluLookAt(3,4,2,0,0,0,0,0,1);


        DrawCube();
        glTranslated(0,0,2.2);
        glRotated(60,1,1,0);

        DrawRocket();

        glFlush();
        SDL_GL_SwapBuffers();

    }

    return 0;
}

void DrawRocket()
{

    GLUquadric* params = gluNewQuadric();
    glColor3ub(255,255,255);

    glRotated(angleZ,0,0,1);
    //glRotated(angleX,1,0,0);
    glBindTexture(GL_TEXTURE_2D,textureTop);
    gluQuadricTexture(params,GL_TRUE);
    gluQuadricDrawStyle(params,GLU_FILL);
    gluCylinder(params,0.5,0,1.6,20,1);

    glTranslated(0,0,-1.05);
    glBindTexture(GL_TEXTURE_2D,textureMiddle);

    gluCylinder(params,0.15,0.5,1.05,20,1);
    glTranslated(0,0,-0.25);
    glBindTexture(GL_TEXTURE_2D,textureBottom);

    gluCylinder(params,0.3,0.15,0.25,20,1);

    glBindTexture(GL_TEXTURE_2D,textureMotor);

    gluDisk(params,0,0.3,20,1);



//        gluSphere(params,0.75,20,20);

    gluDeleteQuadric(params);


}
void DrawCube()
{
    glPushMatrix();


    //glRotated(angleZ,0,0,1);
    //glRotated(angleX,1,0,0);

    glBindTexture(GL_TEXTURE_2D, texture1);

    glBegin(GL_QUADS);
    glColor3ub(255,255,255);
    glTexCoord2d(0,1);
    glVertex3d(1,1,1);
    glTexCoord2d(0,0);
    glVertex3d(1,1,-1);
    glTexCoord2d(1,0);
    glVertex3d(-1,1,-1);
    glTexCoord2d(1,1);
    glVertex3d(-1,1,1);

    glEnd();
    glBindTexture(GL_TEXTURE_2D, 0);
    glBegin(GL_QUADS);

    glColor3ub(0,255,0); //face verte
    glVertex3d(1,-1,1);
    glVertex3d(1,-1,-1);
    glVertex3d(1,1,-1);
    glVertex3d(1,1,1);

    glColor3ub(0,0,255); //face bleue
    glVertex3d(-1,-1,1);
    glVertex3d(-1,-1,-1);
    glVertex3d(1,-1,-1);
    glVertex3d(1,-1,1);

    glColor3ub(255,255,0); //face jaune
    glVertex3d(-1,1,1);
    glVertex3d(-1,1,-1);
    glVertex3d(-1,-1,-1);
    glVertex3d(-1,-1,1);

    glColor3ub(0,255,255); //face cyan
    glVertex3d(1,1,-1);
    glVertex3d(1,-1,-1);
    glVertex3d(-1,-1,-1);
    glVertex3d(-1,1,-1);

    glColor3ub(255,0,255); //face magenta
    glVertex3d(1,-1,1);
    glVertex3d(1,1,1);
    glVertex3d(-1,1,1);
    glVertex3d(-1,-1,1);

    glEnd();
    glPopMatrix();
    glBindTexture(GL_TEXTURE_2D, texture2);

    glBegin(GL_QUADS);
    glColor3ub(255,0,0);
    glTexCoord2i(0,0);
    glVertex3i(-10,-10,-1);
    glColor3ub(0,255,0);
    glTexCoord2i(10,0);
    glVertex3i(10,-10,-1);
    glColor3ub(255,255,0);
    glTexCoord2i(10,10);
    glVertex3i(10,10,-1);
    glColor3ub(255,0,255);
    glTexCoord2i(0,10);
    glVertex3i(-10,10,-1);
    glEnd();


}
